import { segment } from "oicq"
import fetch from "node-fetch"
import lodash from 'lodash'
import fs from 'fs'
import dayjs from 'dayjs'

let datapath = `./data/quanzhong`

fs.exists("./data/quanzhong",function (exists) {
  if (!exists) fs.mkdirSync("./data/quanzhong")
})

export class example extends plugin {
	constructor() {
		super({
			name: '查权重',
			dsc: '查询qq账号权重',
			event: 'message',
			priority: 10,
			rule: [
				{
				reg: "^#*查权重(.*)$",
				fnc: 'cqz'
				},
			]
		})
	}

	async cqz(e) {
		let qq = e.message.filter(item => item.type == 'at')?.map(item => item?.qq)
		if (lodash.isEmpty(qq)) {
			qq = e.msg.match(/\d+/g)
		}
		if (!qq) qq = e.user_id;
		
		let response = await fetch(`http://tc.tfkapi.top/API/qqqz.php?type=json&qq=${qq}`);
		let res = await response.json();
		
		try{
			fs.readFileSync(datapath+`/${qq}.json`)
		}catch(err){
			let raw = {"qz":"暂无历史权重","time":"暂无历史记录"}
			fs.writeFileSync(datapath+`/${qq}.json`, JSON.stringify(raw))
			e.reply('该qq号第一次查询,已创建新纪录',true)
		}
		
		let lastdata = JSON.parse(fs.readFileSync(datapath+`/${qq}.json`));
		console.log(lastdata)
		let msg = [
			"查询结果：",res.msg, "\n",
			`查询的qq号：${qq}`, "\n",
			"权重：", res.qz, "\n",
			"上次查询时的权重：",lastdata.qz,"\n",
			"上次查询时间：",lastdata.time//"权重越低越有可能封号，权重低时别涩涩啦"
		];
		e.reply(msg);
		var time = dayjs().format('YYYY/MM/DD HH:mm:ss')
		var newdata = {"qz":res.qz, "time":time};
		var newdatajson = JSON.stringify(newdata);
		fs.writeFileSync(datapath+`/${qq}.json`, newdatajson)
	}
	
}